package com.guo.loadinganim;

import android.app.Activity;
import android.util.Log;
import android.view.View;
import android.view.Window;
import android.view.WindowManager;

import java.lang.annotation.ElementType;
import java.lang.reflect.Field;
import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;
import java.util.ArrayList;

public class ViewManager {
    private static final String TAG = "ViewManager";

    /**
     * 基于反射通过wms获取悬浮窗列表
     * @param activity
     * @return
     */
    public static View getFloatView(Activity activity){
        try{
            Class<?> c = Class.forName("android.view.WindowManagerGlobal");
            Method m = c.getDeclaredMethod("getInstance", null);
            Object wmg = m.invoke(null, null);
            Field viewsField = c.getDeclaredField("mViews");
            viewsField.setAccessible(true);
            Object objectViews = viewsField.get(wmg);
            if(objectViews instanceof ArrayList){
                ArrayList<View> views = (ArrayList<View>)objectViews;
                if(null != views){
                    Log.d(TAG, "views size = " + views.size());
                }
            }
        }catch (ClassNotFoundException c){
            Log.d(TAG, "class not found");
        }catch (NoSuchFieldException e){
            Log.d(TAG, "no such field");
        }catch (NoSuchMethodException m){
            Log.d(TAG, "no such method");
        }catch (IllegalAccessException i){
            Log.d(TAG, "illegal access exception");
        }catch (InvocationTargetException i){
            Log.d(TAG, "invocation target exception");
        }
        return null;
    }
}
